import 'package:flutter/material.dart';
import 'package:flutter_application_1/models/Cart.dart';
import 'package:flutter_application_1/provider/cart_provider.dart';
import 'package:provider/provider.dart';

class CartPage extends StatefulWidget {
  @override
  _CartPageState createState() => _CartPageState();
}

class _CartPageState extends State<CartPage> {
  late CartProvider _cartProvider;

  Future<void> _onRefresh() async {
    await _cartProvider.getCartList();
  }

  @override
  void initState() {
    super.initState();
    _cartProvider = Provider.of<CartProvider>(context, listen: false);

    _cartProvider.getCartList();
  }

  @override
  Widget build(BuildContext context) {
    _cartProvider = Provider.of<CartProvider>(context);

    return Scaffold(
      appBar: AppBar(
        title: Text('购物车(${_cartProvider.cartList.length})'),
      ),
      body: RefreshIndicator(
        onRefresh: _onRefresh,
        child: ListView.builder(
          itemCount: _cartProvider.cartList.length,
          itemBuilder: (BuildContext context, int index) {
            Cart cart = _cartProvider.cartList[index];
            return ListTile(
              title: Text('${cart.name}'),
              trailing: Text('X ${cart.number}'),
            );
          },
        ),
      ),
    );
  }
}
